package com.sinux.sshUser.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sinux.sshUser.entity.Role;
import com.sinux.sshUser.entity.User;
import com.sinux.sshUser.service.RoleServiceI;



/**
 * @author WangGan
 * @version 2017年5月23日 下午11:13:49 
 * 
 */

@Controller
public class RoleController {

	@Autowired
	private RoleServiceI roleService;
	
	/***分页角色列表***/
	@ResponseBody
	@RequestMapping("/roletab")
	public Map<String ,Object> roleindex(HttpServletRequest request, HttpServletResponse response, Model model) {
		Map<String ,Object> map=new HashMap<String ,Object>();
		int pageIndex =Integer.valueOf(request.getParameter("page")); //第几页的数据  
	    int pageSize = Integer.valueOf(request.getParameter("rows")); //每页多少条数据  
	    List<Role> roles=roleService.findRolePageing(pageIndex,pageSize);
		//model.addAttribute("userList", userService.findAllUserList()); 
	    int total=roleService.findAllRoleList().size();
	    map.put("total", total);
	    map.put("rows", roles);
		return map;
	}
	
	//模糊搜索 分页
	@ResponseBody
	@RequestMapping("/seachroletab")
	public Map<String ,Object> seachroleindex(HttpServletRequest request, HttpServletResponse response, Model model) {
		Map<String ,Object> map=new HashMap<>();
		int pageIndex =Integer.valueOf(request.getParameter("page")); //第几页的数据  
	    int pageSize = Integer.valueOf(request.getParameter("rows")); //每页多少条数据  
		String value=request.getParameter("txt");
		List<Role> roles=roleService.findRoleList(value, pageIndex, pageSize);
		//model.addAttribute("roleList", roleService.findRoleList(txt));
		int total=roleService.findRoleList(value).size();
		map.put("total", total);
	    map.put("rows", roles);
		return map;
	}
	//角色弹出框
	@RequestMapping("/oper_rodg")
	public String asfa(HttpServletRequest request,HttpServletResponse response,Model model){
		return "/include/operdg/oper_role_log";
	}
	//角色列表
	@ResponseBody
	@RequestMapping("/role_log")
	public Map<String ,Object> rolelog(HttpServletRequest request, HttpServletResponse response, Model model) {
		
		Map<String ,Object> map=new HashMap<String ,Object>();
	    List<Role> roles=roleService.findAllRoleList();
	    map.put("rows", roles);
		return map;
	}
	
	/**查询角色内的用户*/
	@ResponseBody
	@RequestMapping("/sel_user_role")
	public Map<String ,Object> selUserinDepart(HttpServletRequest request,HttpServletResponse response,Model model){
		Map<String, Object> map=new HashMap<String, Object>();
		int page =Integer.valueOf(request.getParameter("page")); //第几页的数据  
	    int rows = Integer.valueOf(request.getParameter("rows")); //每页多少条数据
	    String roid=request.getParameter("roid");//获取部门ID
	    System.out.println("page:"+page+" rows:"+rows+" deid:"+roid);
	    Set<User> users=roleService.get(roid).getUsers();
	    List<User> list=roleService.findRoleUsers(roid, page, rows);
		map.put("total", users.size());
	    map.put("rows", list);
		return map;
	}
	
	//从某个角色中移除用户
		@ResponseBody
		@RequestMapping("/remove_user_role")
		public Map<String, Object> removeUser(HttpServletRequest request,HttpServletResponse response,Model model){
			Map<String, Object> map=new HashMap<String,Object>();
			Map<String, String[]> params=request.getParameterMap();
			String roid=params.get("roid")[0];
			String ss=params.get("uid")[0];
			String[] uids=ss.split(",");
			boolean isTAG=roleService.removeRoleUsers(roid,uids);
			if(isTAG){
				map.put("success", true);
				map.put("msg", "成功");
			}else{
				map.put("success", false);
				map.put("msg", "失败");
			}
			return map;
		}

}
